class Event < ActiveRecord::Base
  validates :title, :presence => true
  
  has_many :videos
  has_many :articles
  has_many :images
  has_many :news
  
  belongs_to :user
  belongs_to :event_status
  has_one :location, :dependent => :destroy
  
  def articles_count
    articles.count
  end
  
  def self.filter( filters = {} )
	@where = ''

	if filters[:text] && !filters[:text].empty?
		@where += " (title LIKE '%#{ filters[:text] }%' OR" +
					" description LIKE '%#{ filters[:text] }%'" +					
					") AND"
	end
	
	if !@where.empty?
		@where += " 1=1"
	end
	
	self.where(@where)
  end
  
  def set_status(status)
    self.event_status = EventStatus.get(status)
  end
  
  def chairman_edit?(current_user)
    event_status != EventStatus.get('ASSIGNED') && user == current_user
  end
  
end
